package com.ryan.smile;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ryan.smile.model.TitleModel;
import com.ryan.smile.util.FileUtil;

public class ListActivity extends BaseActivity {

    private ListView mList;
    private ListAdapter mAdapter;
    private int mStage;
    
    @Override
    protected void doCreate(Bundle savedInstanceState) {
        mStage = getIntent().getIntExtra("stage", 0);
        setContentView(R.layout.list);
        
        mList = (ListView) findViewById(R.id.list);
        mAdapter = new ListAdapter();
        mList.setAdapter(mAdapter);
        mList.setCacheColorHint(0);
        mList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ListActivity.this,ViewActivity.class);
                intent.putExtra("stage", mStage);
                intent.putExtra("seek", id);
                intent.putExtra("line", position);
                intent.putExtra("size", mAdapter.getCount());
                intent.putParcelableArrayListExtra("seekList", mSeekList);
                startActivity(intent);
            }
        });
    }
    
    private ArrayList<TitleModel> mSeekList;
    
    private class ListAdapter extends BaseAdapter{
        private ArrayList<TitleModel> rst;
        
        private ListAdapter(){
            rst = FileUtil.getTitle(ListActivity.this, mStage);
            mSeekList = new ArrayList<TitleModel>();
            for (TitleModel tm : rst) {
                TitleModel t = new TitleModel();
                t.setLastSeek(tm.getLastSeek());
                t.setSeek(tm.getSeek());
                t.setNextSeek(tm.getNextSeek());
                mSeekList.add(t);
            }
        }
        
        @Override
        public int getCount() {
            return rst.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return rst.get(position).getSeek();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            Holder holder;
            if(convertView==null){
                v = ListActivity.this.getLayoutInflater().inflate(R.layout.list_item, null);
                holder = new Holder();
                holder.desc = (TextView) v.findViewById(R.id.desc);
                v.setTag(holder);
            }else{
                v = convertView;
                holder = (Holder) v.getTag();
            }
            holder.desc.setText(rst.get(position).getDesc());
            return v;
        }
        
        private class Holder {
            private TextView desc;
        }
        
    }
}
